﻿// *****************************************************************************************************
//   Copyright (c) 2020, HP Development Company, L.P. All rights reserved.
//   This software contains confidential and proprietary information of HP.
//   The user of this software agrees not to disclose, disseminate or copy
//   such Confidential Information and shall use the software only in accordance
//   with the terms of the license agreement the user entered into with HP. 
// *****************************************************************************************************

namespace LedControl.HidControl
{
    using System;

    public static class IteHelper
    {
        private static HidHelper ITELedHid = null;

        private const ushort VID = 0x048D;
        private static readonly ushort[] PIDs = new ushort[] { 0x8295, 0x8233};
        private const ushort UsagePage = 0xFF89;
        private const ushort Usage = 0xCC;

        private const uint HIDAccess = 0;
        private const uint HIDShareMode = HidHelper.FileShareRead | HidHelper.FileShareWrite;
        private const byte ControlCode_ChangeLedState = 0x1;

        static IteHelper()
        {
            if (ITELedHid == null)
            {
                foreach(var pid in PIDs)
                {
                    ITELedHid = new HidHelper(VID, pid, UsagePage, Usage, HIDAccess, HIDShareMode);
                    ITELedHid.Initilize();
                    if (ITELedHid.IsExist)
                    {
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// Check if the ITE HID device exist or not
        /// </summary>
        /// <returns>Exist or not</returns>
        public static bool IsExist()
        {
            return ITELedHid.IsExist;
        }

        /// <summary>
        /// Set Led state
        /// </summary>
        /// <param name="ledState">LED State</param>
        /// <returns>Success or not</returns>
        public static bool SetLed(byte ledState)
        {
            if (ITELedHid.IsExist)
            {
                return ITELedHid.SetFeature(Convert.ToByte(Usage), ControlCode_ChangeLedState, ledState);
            }
            return false;
        }
    }
}
